package security.LoginSystem.Test;

import static org.junit.Assert.*;

import java.net.InetAddress;
import java.net.UnknownHostException;

import org.junit.Test;

import security.Authentication.Encrypted.EncryptedHandler;
import security.Authentication.Encrypted.EncryptedRequest;
import security.Authentication.Guest.GuestHandler;
import security.Authentication.Guest.GuestRequest;
import security.Authentication.Unknown.UnknownRequest;
import security.Authentication.UserPass.UserPassHandler;
import security.Authentication.UserPass.UserPassRequest;
import security.Authorization.UserRole.UserRole;
import security.IntrusionDetection.Base.Email;
import security.IntrusionDetection.BruteforceIntrusion.BruteForceIntrusion;
import security.IntrusionDetection.IPInstrusion.IPIntrusion;
import security.LoginSystem.Base.LoginSystem;

/**
 * Test cases for the whole login system
 * @author Andreas Carmvall
 */
public class LoginSystemTest {

	/**
	 * Test the whole login system
	 * @throws UnknownHostException the local host IP is needed and was not able to be read
	 */
	@Test
	public void testLoginSystem() throws UnknownHostException {
		LoginSystem loginSystem = new LoginSystem();
		InetAddress localHost = InetAddress.getLocalHost();
		UnknownRequest unknownRequest = new UnknownRequest(localHost);
		GuestRequest guestRequest = new GuestRequest(localHost);
		UserPassRequest userPassRequest =
				new UserPassRequest(localHost, "TestUser", "Abc123!");
		EncryptedRequest encryptedRequest =
				new EncryptedRequest(localHost, "QwertyUser", "Qwerty1.");
		
		// Add the authentication, authorization and intrusion detection to the system
		loginSystem.setAuthorization(new UserRole());
		loginSystem.addAuthentication(new GuestHandler());
		loginSystem.addIntrusionDetection(new IPIntrusion(new Email()));
		loginSystem.addIntrusionDetection(new BruteForceIntrusion());
		loginSystem.addAuthentication(new UserPassHandler());
		loginSystem.addAuthentication(new EncryptedHandler());

		// Test that the correct authorization is made
		loginSystem.login(unknownRequest);
		loginSystem.login(guestRequest);
		loginSystem.login(userPassRequest);
		loginSystem.login(encryptedRequest);
		
		loginSystem.clearChain();

		// Test that the chain is cleared
		loginSystem.login(unknownRequest);
		loginSystem.login(guestRequest);
		loginSystem.login(userPassRequest);
		loginSystem.login(encryptedRequest);
		
		assertTrue(true);
	}

}
